import {
  RECORD_USERINFO,
  UPDATE_USERINFO,
  SAVE_RESET_PW,
  LOGOUT,
  NEXTPAGE,
  SAVE_ARTICLE,
  SAVE_ROOM,
  UPDATE_AVATAR,
  UPDATE_LIKE,
  UPDATE_UPVOTE,
  SAVE_COMMENT,
  SAVE_COMMENT2,
  SAVE_ALBUM,
  SAVE_ROOM_NAME,
} from './mutation-types.js'

import {
	setStore,
	getStore,
} from '../config/mUtils'

export default {
  // 记录用户信息
  [RECORD_USERINFO](state, info) {
    state.token = info.result[0].token;
    state.userInfo.email = info.result[0].email;
    state.userInfo.userId = info.result[1].userId;
    state.userInfo.nick = info.result[1].nick;
    state.userInfo.avatar = info.result[1].avatar;
    state.userInfo.signature = info.result[1].signature;
    state.userInfo.sex = info.result[1].sex;
    state.userInfo.age = info.result[1].age;
    state.userInfo.mobile = info.result[1].mobile;
    state.userInfo.birthday = info.result[1].birthday;
    state.userInfo.city = info.result[1].city;
    state.userInfo.vip = info.result[1].vip;
    state.userInfo.interest = info.result[1].interest;
    state.userInfo.show = info.result[1].show;
    state.loginStatus = true;

    localStorage.token = info.result[0].token;
    localStorage.email = info.result[0].email;
    localStorage.userId = info.result[1].userId;
    localStorage.nick = info.result[1].nick;
    localStorage.avatar = info.result[1].avatar;
    localStorage.signature = info.result[1].signature;
    localStorage.sex = info.result[1].sex;
    localStorage.age = info.result[1].age;
    localStorage.mobile = info.result[1].mobile;
    localStorage.birthday = info.result[1].birthday;
    localStorage.city = info.result[1].city;
    localStorage.vip = info.result[1].vip;
    localStorage.interest = info.result[1].interest;
    localStorage.show = info.result[1].show;

    localStorage.loginStatus = state.loginStatus;
  },

  [UPDATE_USERINFO](state, info){
    state.userInfo.signature = info.signature;
    state.userInfo.sex = info.sex;
    state.userInfo.age = info.age;
    state.userInfo.mobile = info.mobile;
    state.userInfo.birthday = info.birthday;
    state.userInfo.city = info.city;
    state.userInfo.vip = info.vip;
    state.userInfo.interest = info.interest;
    state.userInfo.show = info.show;

    localStorage.signature = info.signature;
    localStorage.sex = info.sex;
    localStorage.age = info.age;
    localStorage.mobile = info.mobile;
    localStorage.birthday = info.birthday;
    localStorage.city = info.city;
    localStorage.vip = info.vip;
    localStorage.interest = info.interest;
    localStorage.show = info.show;
  },

  // 保存空间信息
  [SAVE_RESET_PW](state, resetPwInfo) {
    localStorage.removeItem('resetPwInfo');
    state.resetPwInfo = resetPwInfo;
    localStorage.resetPwInfo = JSON.stringify(resetPwInfo);
  },

  // 登出
  [LOGOUT](state) {
    localStorage.removeItem('token');
    localStorage.removeItem('email');
    localStorage.removeItem('userId');
    localStorage.removeItem('avatar');
    localStorage.removeItem('nick');
    localStorage.removeItem('loginStatus');
    localStorage.removeItem('articleDetail');
    localStorage.removeItem('roomDetail');
    localStorage.removeItem('commentDetail');
    localStorage.removeItem('albumDetail');
    localStorage.removeItem('roomName');
    state.token = null;
    state.userInfo.email = "";
    state.userInfo.userId = "";
    state.userInfo.avatar = "";
    state.userInfo.nick = "";
    state.loginStatus = "";
    state.articleDetail = null;
    state.roomDetail = null;
    state.commentDetail = null;
    state.albumDetail = null;
    state.roomName = "";
    window.window.sessionStorage.clear();
  },

  // 页面跳转刷新时
  [NEXTPAGE](state, data) {

    state.token = data.getItem('token');
    state.userInfo.email = data.getItem('email');
    state.userInfo.userId = data.getItem('userId');
    state.userInfo.avatar = data.getItem('avatar');
    state.userInfo.nick = data.getItem('nick');
    state.articleDetail =  JSON.parse(data.getItem('articleDetail'));

    state.roomDetail =  JSON.parse(data.getItem('roomDetail'));
    state.commentDetail =  JSON.parse(data.getItem('commentDetail'));
    state.albumDetail =  JSON.parse(data.getItem('albumDetail'));
    state.roomName = data.getItem('roomName');
  },

  // 保存文章信息
  [SAVE_ARTICLE](state, articleDetail) {
    localStorage.removeItem('articleDetail');
    state.articleDetail = articleDetail;
    localStorage.articleDetail = JSON.stringify(articleDetail);

  },

  // 保存空间信息
  [SAVE_ROOM](state, roomDetail) {
    localStorage.removeItem('roomDetail');
    state.roomDetail = roomDetail;
    localStorage.roomDetail = JSON.stringify(roomDetail);
  },

  // 保存空间信息
  [UPDATE_AVATAR](state, avatar) {
    state.userInfo.avatar = avatar;
    localStorage.avatar = avatar;
  },

  // 更新收藏状态
  [UPDATE_LIKE](state) {
    localStorage.removeItem('articleDetail');
    state.articleDetail.like = !state.articleDetail.like ;
    localStorage.articleDetail = JSON.stringify(state.articleDetail);
  },

  // 更新收藏状态
  [UPDATE_UPVOTE](state) {
    localStorage.removeItem('articleDetail');
    state.articleDetail.upvote = !state.articleDetail.upvote ;
    localStorage.articleDetail = JSON.stringify(state.articleDetail);
  },

  // 保存文章页面中的评论信息
  [SAVE_COMMENT](state, commentDetail) {
    localStorage.removeItem('commentDetail');
    state.commentDetail = commentDetail;
    localStorage.commentDetail = JSON.stringify(commentDetail);
  },

  // 保存评论页面中的评论信息
  [SAVE_COMMENT2](state, commentDetail) {
    localStorage.removeItem('commentDetail2');
    state.commentDetail2 = commentDetail;
    localStorage.commentDetail2 = JSON.stringify(commentDetail);
  },

  // 保存空间信息
  [SAVE_ALBUM](state, albumDetail) {
    localStorage.removeItem('albumDetail');
    state.albumDetail = albumDetail;
    localStorage.albumDetail = JSON.stringify(albumDetail);
  },

  // 保存空间信息
  [SAVE_ROOM_NAME](state, roomName) {
    localStorage.removeItem('roomName');
    state.roomName = roomName;
    localStorage.roomName = roomName;
  },

}
